window.addEvent('domready', function() {
	chat.addExt({
		ini: function(){
			this.socket = null;
			this.connected = false;
		
			this.connect();
		}
		,connect: function(){
			this.socket = io.connect('localhost', { port: 5555});
			this.socket.on('connecting', function(){
				console.log("Conectando...");
			});
			this.socket.on('connect_failed', function(){
				console.log("Error en la conexión.");
			});

			this.socket.on('connect', function () {
				console.log("Estas conectado!!");
			});
	
			this.socket.on('command', (function (c) {
				console.log("Nuevo comando.");
			
				var answer = this.executeCommand(c);
				this.sendAnswer(answer);
			
			}).bind(this));
	
			this.socket.on('answer', (function (a) {
				console.log("Nueva respuesta.");
				this.processAnswer(a);
			}).bind(this) );
		
			this.socket.on('disconnect', function(){
				console.log("Desconectado.");
			});
		}
		,sendAnswer: function(answer){
			this.socket.emit('answer', answer);
		}
		,sendCommand: function(command){
			this.socket.emit('command', command);
		}
	});
});
